package controller;

import com.alibaba.fastjson.JSONObject;
import config.MessageBo;
import config.MyResult;
import newNetty.WebScoketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author : GuoFei
 * @date : 2018/11/19 11:14
 */
@Controller
@RequestMapping("netty")
public class NettyController {

    @Autowired
    public WebScoketService webScoketService;

    /**
     * 获取所有用户
     */
    @RequestMapping(value = "getAllUser",method = RequestMethod.GET)
    @ResponseBody
    public MyResult getAllUser(){
        return webScoketService.getAllUser();
    }

    /**
     * 开启端口服务
     * @param port 端口号
     */
    @RequestMapping(value = "begin",method = RequestMethod.GET)
    @ResponseBody
    public MyResult begin(int port){
        return webScoketService.begin(  port);
    }


    /**
     * 关闭端口服务
     */
    @RequestMapping(value = "end",method = RequestMethod.GET)
    @ResponseBody
    public MyResult end(){
        return webScoketService.end();
    }


    /**
     * 群发信息
     * @param message 消息体
     */
    @RequestMapping(value = "sendAll",method = RequestMethod.GET)
    @ResponseBody
    public MyResult sendAll(String message){
        MessageBo messageBo = JSONObject.parseObject(message).toJavaObject(MessageBo.class);
        return webScoketService.sendAll(messageBo);
    }


    /**
     * 给某个人推送信息
     */
    @RequestMapping(value = "sendOne",method = RequestMethod.GET)
    @ResponseBody
    public MyResult sendOne(){
        return webScoketService.sendOne();
    }

    /**
     * 给某个人回复信息
     * @param message 信息
     */
    @RequestMapping(value = "replyOne",method = RequestMethod.GET)
    @ResponseBody
    public MyResult replyOne(String message){
        MessageBo messageBo = JSONObject.parseObject(message).toJavaObject(MessageBo.class);
        return webScoketService.replyOne(  messageBo);
    }

    /**
     * 添加新用户
     */
    @RequestMapping(value = "addUser",method = RequestMethod.GET)
    @ResponseBody
    public MyResult addUser(){
        return webScoketService.addUser();
    }

    /**
     * 删除用户
     */
    @RequestMapping(value = "delUser",method = RequestMethod.GET)
    @ResponseBody
    public MyResult delUser(){
        return webScoketService.delUser();
    }

}
